package com.example.mytk2_18;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    TextView tv_state;
    EditText ip_edit;
    EditText ip_port;
    Button bt_connect;
    Button bt_close;

    Button bt_stop,bt_up,bt_left , bt_right , bt_down,bt_saodi;
    EditText car_state;

    String ip;
    int port;
    Socket ss;

    DataOutputStream dataOutputStream=null;

    byte zl[]=new byte[]{(byte)0x55,(byte)0xBB,(byte)0x00};



    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_state=(TextView)this.findViewById(R.id.tv_state);
        ip_edit=(EditText)this.findViewById(R.id.ip_edit);
        ip_port=(EditText)this.findViewById(R.id.ip_port);
        bt_connect=(Button)this.findViewById(R.id.bt_connect);
        bt_close=(Button)this.findViewById(R.id.bt_close);
        bt_stop=(Button)this.findViewById(R.id.bt_stop);
        bt_up=(Button)this.findViewById(R.id.bt_up);
        bt_left=(Button)this.findViewById(R.id.bt_left);
        bt_right=(Button)this.findViewById(R.id.bt_right);
        bt_down=(Button)this.findViewById(R.id.bt_down);
        bt_saodi=(Button)this.findViewById(R.id.bt_saodi);
        car_state=(EditText)this.findViewById(R.id.car_state);
        car_state.setText(" ");
        bt_connect.setOnClickListener(this);
        bt_close.setOnClickListener(this);
        bt_stop.setOnClickListener(this);
        bt_up.setOnClickListener(this);
        bt_left.setOnClickListener(this);
        bt_right.setOnClickListener(this);
        bt_down.setOnClickListener(this);
        bt_saodi.setOnClickListener(this);

    }

    //连接服务器
    void connectServer()
    {
        //任务3_2 ：从IP地址编辑框ip_edit获取IP地址并保存到变量ip
        ip = ip_edit.getText().toString().trim();

        //结束任务3_2
        port = Integer.parseInt(ip_port.getText().toString().trim());

        new Thread(new Runnable() {
            @Override
            public void run() {
                //连接网路，如果不产生异常，说明连接成功，设置tv_state文本为已连接
                try {
                    //任务3_3 ：实现建立Socket网络连接和数据输出流DataOutputStream。
                    ss = new Socket(ip, port);

                    //结束任务3_3
                    //数据输出流DataInputStream
                    dataOutputStream = new DataOutputStream(ss.getOutputStream());

                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            //任务3_4 ：实现文本框tv_state显示为"已连接"状态

                            tv_state.setText("已连接");

                            //结束任务3_4
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //关闭连接
    void closeConnect()
    {
        try {
            ss.close();
            tv_state.setText("网络断开");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    void stopCar()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                zl[2]=(byte)0x00;
                try {
                    dataOutputStream.write(zl);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            car_state.setText("停止");
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    void Qcar()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                zl[2]=(byte)0x01;
                try {
                    dataOutputStream.write(zl);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            car_state.setText("向前");
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    void Hcar()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                zl[2]=(byte)0x02;
                try {
                    dataOutputStream.write(zl);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            car_state.setText("向后");
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    void Zcar()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                zl[2]=(byte)0x03;
                try {
                    dataOutputStream.write(zl);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            car_state.setText("左转");
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    void Ycar()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                zl[2]=(byte)0x04;
                try {
                    dataOutputStream.write(zl);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            car_state.setText("右转");
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    void SDcar()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                zl[2]=(byte)0x01;
                try {
                    dataOutputStream.write(zl);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            car_state.setText("扫地");
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    @Override
    public void onClick(View view) {
        car_state.setText("");
        switch (view.getId())
        {
            case R.id.bt_connect:
                connectServer();
                break;
            case R.id.bt_close:
                closeConnect();
                break;
            case R.id.bt_stop:
                stopCar();
                break;
            case R.id.bt_up:
                Qcar();
                break;
            case R.id.bt_down:
                Zcar();
                break;
            case R.id.bt_left:
                SDcar();
                break;
            case R.id.bt_right:
                Ycar();
                break;
            case R.id.bt_saodi:
                Hcar();
                break;
        }
    }

}
